<template>
	<div class="container">
		<el-dialog
			title="设置角色"
			:close-on-click-modal="false"
			:close-on-press-escape="false"
			destroy-on-close
			v-model="isShowDialog"
			width="320px"
		>
			<el-form :model="ruleForm" ref="userForm" size="small" label-width="80px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="账号" prop="account">
							<el-input v-model="ruleForm.account" placeholder="请输入账号" disabled></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="昵称" prop="nick_name">
							<el-input v-model="ruleForm.nick_name" placeholder="请输入昵称" disabled></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="设置角色" prop="role_id">
							<el-cascader
								@change="changeRole"
								style="width:100%"
								placeholder="请选择角色"
								v-model="ruleForm.role_id"
								:props="{
									value: 'ID',
									label: 'RoleName',
									checkStrictly: true
								}"
								:options="role_list"
								filterable
								clearable
							/>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="small">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="small">提 交</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts">
import { reactive, toRefs, watch, ref, defineComponent } from 'vue';
import { ElMessageBox } from 'element-plus';
import { getList } from '/@/api/role/index'
import { edit } from '/@/api/user/index'
export default defineComponent({
	name: 'EditUser',
	setup(props: any, context: any) {
		const state = reactive({
			isShowDialog: false,
			ruleForm: {
				id: 0,
				account: "",
				nick_name: "",
				role_id: [],
			},
			role_list: []
		});
		const userForm = ref();
		watch(() => state, (value) => {

		})
		const getRoles = () => {
			getList({}).then((res) => {
				state.role_list = res.list;
			})

		};
		getRoles();
		const changeRole = (checkValue: any) => {

		};
		// 打开弹窗
		const openDialog = (row?: object) => {
			state.isShowDialog = true;
			state.ruleForm.id = row?.ID;
			state.ruleForm.account = row?.Account;
			state.ruleForm.nick_name = row?.NickName;
			state.ruleForm.role_id = [row?.RoleId]
		};
		// 关闭弹窗
		const closeDialog = (row?: object) => {
			state.isShowDialog = false;
		};
		// 取消
		const onCancel = () => {
			closeDialog();
			initForm();
		};
		const initForm = () => {
			userForm.value.resetFields();
		};
		// 新增
		const onSubmit = () => {
			state.ruleForm.role_id = state.ruleForm.role_id[0]
			edit(state.ruleForm).then((res) => {
				if (res.code === 200) {
					ElMessageBox.alert(res.msg, '提示', {}).then(() => {
						// 刷新父级数据
						context.emit("sendRefresh");
						closeDialog(); // 关闭弹窗
						initForm();
					}).catch(() => { });
				}
			})
		};
		return {
			changeRole,
			openDialog,
			closeDialog,
			onCancel,
			onSubmit,
			...toRefs(state),
			userForm
		};
	},
});
</script>